package com.yplan.dm;

import io.netty.util.internal.StringUtil;

import javax.swing.*;
import javax.swing.text.DefaultCaret;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

public class danmu {
    public JPanel panel1;
    public JTextField aTextField;
    public JTextArea textArea1;
    public JTextField ipTextField;
    public JButton lButton;
    public JButton rButton;
    public JTextField urlTextField;
    private JScrollPane scrollpane;
    private JRadioButton douyinrb;
    private JRadioButton bilibilirb;

    public static void main(String[] args) {
        JFrame frame = new JFrame("弹幕助手");
        URL resource = danmu.class.getClassLoader().getResource("abc.png");
        Image image = new ImageIcon(resource).getImage();
        frame.setIconImage(image);
        final danmu danmu = new danmu();
        danmu.panel1.setMinimumSize(new Dimension(800, 600));
        DanmuHelper danmuHelper = new DanmuHelper("", 80, danmu);
//        danmuHelper.setInfo(danmu.ipTextField.getText(), Integer.valueOf(danmu.aTextField.getText()), "https://live.douyin.com/124742367599",0);
//        danmuHelper.start();
        frame.setContentPane(danmu.panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        danmu.lButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (StringUtil.isNullOrEmpty(danmu.ipTextField.getText()) || StringUtil.isNullOrEmpty(danmu.aTextField.getText())
                        || StringUtil.isNullOrEmpty(danmu.urlTextField.getText())) {
                    danmu.textArea1.append("请输入游戏socket服务ip||端口号||抖音直播间地址");
                    return;
                }
                int type = 0;
                if (danmu.douyinrb.isSelected()) type = 0;
                else if (danmu.bilibilirb.isSelected()) type = 1;
                danmuHelper.setInfo(danmu.ipTextField.getText(), Integer.valueOf(danmu.aTextField.getText()), danmu.urlTextField.getText(), type);
                danmuHelper.start();
            }
        });
        danmu.rButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                danmuHelper.stop();
            }
        });
        danmu.bilibilirb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (danmu.bilibilirb.isSelected()) {
                    danmu.douyinrb.setSelected(false);
                }
            }
        });
        danmu.bilibilirb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (danmu.douyinrb.isSelected()) {
                    danmu.bilibilirb.setSelected(false);
                }
            }
        });
        frame.setSize(777, 600);
        danmu.textArea1.setLineWrap(true);
        danmu.textArea1.setWrapStyleWord(true);
        DefaultCaret caret = (DefaultCaret) danmu.textArea1.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
//每次需要在JTextArea增加String时,调用如下方法
    }

}
